<?php
    /**
    * Get list of documents belongs to selected user
    */
    if (!defined('EXPONENT')) exit('');
    function _manage_documents_return_empty()
    {
        $ret_object = null;
        $ret_object->total = 0;
        $ret_object->rows=array(); 
        echo json_encode($ret_object);
        return;
    }

    if (exponent_users_isLoggedIn())
    {  
        $user_id=$user->id;
        if (exponent_users_isAdmin() ||  exponent_permissions_check(array("administrate","upload","delete","meeting","meeting_delete","meeting_assistant"),exponent_core_makeLocation("profilemodule","_syscore",""))){
            if (isset($_REQUEST['user_id']))
                $user_id = intval($_REQUEST['user_id']);
        }

        // select user
        $cuser = $db->selectObject("user", "id = {$user_id}");
        if (!isset($cuser->id))
        {
            _manage_documents_return_empty();
        }
        else
        {

            $sql_where = "user_id = {$user_id} ";

            // Build condition for filter
            if (isset($_REQUEST['search']))
                $sql_where.=" AND (name LIKE '%{$_REQUEST['search']}%')";

            if (isset($_REQUEST['datefrom']) && $_REQUEST['datefrom']!="")
            {
                $date_from=strtotime($_REQUEST['datefrom']);
                $sql_where.=" AND (post_date>={$date_from})";
            }

            if (isset($_REQUEST['dateto']) && $_REQUEST['dateto']!="")
            {
                $date_to=strtotime($_REQUEST['dateto']);
                $sql_where.=" AND (post_date<={$date_to})";
            }

            if (isset($_REQUEST['filetype']))
            {
                $type=$_REQUEST['filetype'];
                if ($type == "none")
                    $sql_where.=" ";
                if ($type == "any")
                    $sql_where.=" AND (flv_file <> '' OR pdf_file <>'' OR mp3_file <>'' OR doc_file <>'' OR xls_file <>'' OR ppt_file <>'')";
                else if ($type == "pdf")
                        $sql_where.=" AND (pdf_file <>'')";
                    else if ($type == "ppt")
                            $sql_where.=" AND (ppt_file <>'')";
                        else if ($type == "video")
                                $sql_where.=" AND (flv_file <>'')";
                            else if ($type == "doc")
                                    $sql_where.=" AND (doc_file <>'')";
                                else if ($type == "xls")
                                        $sql_where.=" AND (xls_file <>'')";
                                    else if ($type == "mp3")
                                            $sql_where.=" AND (mp3_file <>'')";
                                    else if ($type == "img")
                                            $sql_where.=" AND (img_file <>'')";
                                        else if ($type == "nofile")
                                                $sql_where.=" AND (flv_file = '') AND (img_file = '') AND (pdf_file ='') AND ( mp3_file ='') AND (doc_file ='') AND (xls_file ='') AND (ppt_file ='')";
            }

            $page=1;
            if (isset($_REQUEST['page']))
                $page=intval($_REQUEST['page']);
            $pageSize=10;

            if (isset($_REQUEST['rows']))
                $pageSize=intval($_REQUEST['rows']);
            $start=($page-1)*$pageSize;

            // sort
            $sort="";
            if (isset($_REQUEST['sortName'])){
                $sort = " ORDER BY {$_REQUEST['sortName']} {$_REQUEST['sortOrder']}";
            }

            $data=$db->selectObjects('profilemodule_documents',"{$sql_where} {$sort} LIMIT {$start},{$pageSize}");

            // preprocess some data
            foreach ($data as $k=>$v){
                $v->post_date_text = date('d/m/Y H:i:s',$v->post_date);
            }

            $ret_object = null;
            $ret_object->total = $db->countObjects('profilemodule_documents',"{$sql_where}");
            $ret_object->rows = $data; 
            echo json_encode($ret_object);
        }

    }

?>